package com.br.hslib.components.hsJCombobox;

import java.awt.Color;  
import java.awt.event.FocusAdapter;  
import java.awt.event.FocusEvent;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.util.Vector;  
  
import javax.swing.JComboBox;

public class HsCombobox extends JComboBox{
	  
	
		
	private static final long serialVersionUID = 1L;
	/* 
     *  fieldList  - campo listado 
     *  valueList  - valor do fieldList 
     *  fieldIndex - campo indice 
     *  valueIndex - valor do fieldIndex  
     */     
    private String table, fieldList, fieldIndex, valueList;         
    private Vector listIndex = new Vector();  
    private Color corAnt;     
  	
	
	/** 
	* Classe extendida do JComboBox 
	* utilizada para vinculo dinamico com fonte de dados 
    * 
    * @param table Nome da Tabela 
    * @param listField Nome do campo que ira aparecer na lista 
    * @param listIndex Nome do campo que ira servir como indice 
    */  

	public HsCombobox(String table, String listField, String listIndex) {  
		super();  
	         
	    this.table      = table;  
	    this.fieldList  = listField;  
	    this.fieldIndex = listIndex;  
	    //fill();  
        addFocusListener(new Focu());  
    }  
	     
	    /** 
	     * preenche combo apartir de uma fonte de dados      
	     */  
	    /*
		private void fill(){         
	        listIndex.clear();  
	         
	       String sQuery =  
	           "SELECT " + fieldList + "," + fieldIndex +  
	           " FROM " + table +  
	           " ORDER BY " + fieldList;  
	       ResultSet rs;        
	       Conexao cnn= new Conexao();  
	       rs = cnn.consulta (sQuery);        
	       try {                       
	           for (int i=0;i< rs.getRow();i++){  
	              addItem(rs.getString(fieldList));  
	              listIndex.add(rs.getString(fieldIndex));  
	              rs.next();               
	          }  
	          rs.close();  
	          cnn.close();     
	       } catch (SQLException e) {  
	           e.printStackTrace();                   
	       }  
	        
	    }  */
	     
	     
	    /** 
	     * seta o combo para a posicao desejada      
	     * @param   value valor do indice 
	     * 
	     * @see #getDBIndex 
	     */  
	    public void setDBIndex(int value){     
	        Integer ret=new Integer(0);  
	         
	        for (int i=0; i< getItemCount();i++){                                                   
	            ret = new Integer(listIndex.get(i).toString());  
	             
	            if (ret.intValue() == value){  
	                setSelectedIndex(i);         
	            }  
	        }  
	    }  
	     
	    /** 
	     * retorna o valor do indice da posicao selecionada      
	     * @return   valor do tipo inteiro 
	     * 
	     * @see #setDBIndex 
	     */  
	    public int getDBIndex(){             
	        return new Integer(listIndex.get(getSelectedIndex()).toString()).intValue();                 
	    }  
	     
	    /** 
	     * Classe interna para eventos de focu 
	     */  
	    private class Focu extends FocusAdapter {  
	  
	        public void focusGained(FocusEvent e){                                       
	            corAnt= getBackground();  
	            setBackground(Color.getHSBColor(70,70,70));  
	        }  
	  
	        public void focusLost(FocusEvent e){                                                 
	            setBackground(corAnt);  
	        }  
	    }  
	     
	   
	
}
